<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
 <%@include file="/common/taglibs.jsp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="${ctx }/js/plugs/jquery-ui-1.10.3.custom/css/mycss/jquery-ui-1.10.3.custom.css" rel="stylesheet" type="text/css" />
<title>奖项设置</title>
<link href="${ctx}/js/plugs/validate/jquery.validate.css" type="text/css" rel="stylesheet" />
<script src="${ctx}/js/plugs/validate/jquery.validate.js" type="text/javascript"></script>
<script src="${ctx}/js/plugs/validate/messages_cn.js" type="text/javascript"></script>
<script src="${ctx }/js/plugs/validate/jquery.metadata.js" type="text/javascript"></script>
<script type="text/javascript">	
function recalculate(obj) {
	value = obj.value.replace(/[^\d]/g,'');
	    if(''!=value){
	        value = parseInt(value);
	    } else {
		   value = 0;
	}
	obj.value=value;
	var partPeople = $('#participants').val();
	var partTimes =  $('#partTimes').val();
	var q1 = $('#qti1').val();
	var q2 = $('#qti2').val();
	var q3 = $('#qti3').val();
	var q4 = $('#qti4').val();
	var q5 = $('#qti5').val();
	var q6 = $('#qti6').val();
	var q7 = $('#qti7').val();
	var q8 = $('#qti8').val();

	var total = partPeople*partTimes;
	var rest = total - q1-q2-q3-q4-q5-q6-q7-q8;
	$('#prizeTotal').text(total);
	$('#prizeRest').text(rest);
	
}
function checkForm(){
	var prizeRest = $('#prizeRest').text();
	var prizeTotal = $('#prizeTotal').text();
	var partPeople = $('#participants').val();
	if (prizeTotal == 0) {
		alert("奖项总数不能等于0")
		return false;
	}
	if (prizeRest != 0) {
		alert("剩余可分配数不能大于0或小于0")
		return false;
	}
	for (i = 1; i < 9; i++) { 
		var prizeTypeId = "prizeType" + i;
		var prizeType = $('#'+prizeTypeId).val();
		if (prizeType == null) {
			alert("请为奖项" + i + "设置奖品类型");
			$('#'+prizeTypeId).focus();
			return false;
		}
	 }
	var ajaxFlag = false;
	var id = $('#lotteryActivityId').val();
	var url = "/sy/lottery/prize/getSurveyAnswerCount.do";
	var data="id="+id;
	$.ajax({
		url:url,
		data:data,
		async:false,
		type:"post",
		success:function(data){
			if((partPeople - data) < 0) {
				alert("当前设置的人数小于试卷应参与人数["+data+"]人");
				ajaxFlag = false;
			} else {
				ajaxFlag = true;
			}
		}
	});
	if (!ajaxFlag) {
		return false;
	}
	if(confirm("核对无误，确认发布?")){
		return true;
	} else {
		return false;
	}
}
</script>
<style type="text/css">
table{
	margin: 0 auto;
	border-collapse: collapse:separate;
	border-spacing:15px 20px;
}
.std{
	text-align:right;
}
.stext{
	width:40px;
}
</style>
</head>
<body>

	<div id="dwBody">
		<div id="dwBodyContent" class="bodyCenter" style="">
			<div id="dwBodyUser">
				<div class="surveyCollectMiddle">
				<div style="border-bottom: 1px solid #DFDFDF;padding: 5px;color: #666565;">奖项设置</div>
				<form id="inputForm" action="${ctx }/sy/lottery/prize/save.do" method="post" onsubmit="return checkForm()">
					<input type="hidden" name="lotteryActivityId" id="lotteryActivityId" value="${id }">
					<table border="0">
						<tr> 
							<td colspan="8" style="text-align:center"><h3>奖项设置</h3></td>
						</tr>
						<tr>
							<td>参与人数:</td>
							<td><input type="text"  id="participants" name="participants" class="stext" value="0" onkeyup="recalculate(this)"></td>
							<td>次数:</td>
							<td><input type="text"  id="partTimes" name="partTimes" class="stext" value="0" onkeyup="recalculate(this)"></td>
							<td>奖项总数:</td>
							<td><font color="blue" id="prizeTotal">0</font></td>
							<td>剩余可分配数:</td>
							<td><font color="red" id="prizeRest">0</font></td>
						</tr>
						<tr>
							<td>奖项一:</td>
							<td><select name="prizeList[0].prizeTypeId"  id="prizeType1"><option value="" disabled selected>请选择类型</option><c:forEach items="${prizeTypeList }" var="en"><option value="${en.id }">${en.typeName }</option></c:forEach></select></td>
							<td class="std" >描述:</td>
							<td><input type="text" name="prizeList[0].description" id="prizeList[0].description"></td>
							<td class="std">数值:</td>
							<td><input type="text" class="stext" name="prizeList[0].value" id="prizeList[0].value" value="0"></td>
							<td class="std">数量:</td>
							<td><input type="text" class="stext" name="prizeList[0].qti" id="qti1" value="0" onkeyup="recalculate(this)"></td>
						</tr>
						<tr>
							<td>奖项二:</td>
							<td><select name="prizeList[1].prizeTypeId"  id="prizeType2"><option value="" disabled selected>请选择类型</option><c:forEach items="${prizeTypeList }" var="en"><option value="${en.id }">${en.typeName }</option></c:forEach></select></td>
							<td class="std" >描述:</td>
							<td><input type="text" name="prizeList[1].description" id="prizeList[1].description"></td>
							<td class="std">数值:</td>
							<td><input type="text" class="stext" name="prizeList[1].value" id="prizeList[1].value" value="0"></td>
							<td class="std">数量:</td>
							<td><input type="text" class="stext" name="prizeList[1].qti" id="qti2" value="0" onkeyup="recalculate(this)"></td>
						</tr>
						<tr>
							<td>奖项三:</td>
							<td><select name="prizeList[2].prizeTypeId"  id="prizeType3"><option value="" disabled selected>请选择类型</option><c:forEach items="${prizeTypeList }" var="en"><option value="${en.id }">${en.typeName }</option></c:forEach></select></td>
							<td class="std" >描述:</td>
							<td><input type="text" name="prizeList[2].description" id="prizeList[2].description"></td>
							<td class="std">数值:</td>
							<td><input type="text" class="stext" name="prizeList[2].value" id="prizeList[2].value" value="0"></td>
							<td class="std">数量:</td>
							<td><input type="text" class="stext" name="prizeList[2].qti" id="qti3" value="0" onkeyup="recalculate(this)"></td>
						</tr>
						<tr>
							<td>奖项四:</td>
							<td><select name="prizeList[3].prizeTypeId"  id="prizeType4"><option value="" disabled selected>请选择类型</option><c:forEach items="${prizeTypeList }" var="en"><option value="${en.id }">${en.typeName }</option></c:forEach></select></td>
							<td class="std" >描述:</td>
							<td><input type="text" name="prizeList[3].description" id="prizeList[3].description"></td>
							<td class="std">数值:</td>
							<td><input type="text" class="stext" name="prizeList[3].value" id="prizeList[3].value" value="0"></td>
							<td class="std">数量:</td>
							<td><input type="text" class="stext" name="prizeList[3].qti" id="qti4" value="0" onkeyup="recalculate(this)"></td>
						</tr>	
						<tr>
							<td>奖项五:</td>
							<td><select name="prizeList[4].prizeTypeId"  id="prizeType5"><option value="" disabled selected>请选择类型</option><c:forEach items="${prizeTypeList }" var="en"><option value="${en.id }">${en.typeName }</option></c:forEach></select></td>
							<td class="std" >描述:</td>
							<td><input type="text" name="prizeList[4].description" id="prizeList[4].description"></td>
							<td class="std">数值:</td>
							<td><input type="text" class="stext" name="prizeList[4].value" id="prizeList[4].value" value="0"></td>
							<td class="std">数量:</td>
							<td><input type="text" class="stext" name="prizeList[4].qti" id="qti5" value="0" onkeyup="recalculate(this)"></td>
						</tr>
						<tr>
							<td>奖项六:</td>
							<td><select name="prizeList[5].prizeTypeId"  id="prizeType6"><option value="" disabled selected>请选择类型</option><c:forEach items="${prizeTypeList }" var="en"><option value="${en.id }">${en.typeName }</option></c:forEach></select></td>
							<td class="std" >描述:</td>
							<td><input type="text" name="prizeList[5].description" id="prizeList[5].description"></td>
							<td class="std">数值:</td>
							<td><input type="text" class="stext" name="prizeList[5].value" id="prizeList[5].value" value="0"></td>
							<td class="std">数量:</td>
							<td><input type="text" class="stext" name="prizeList[5].qti" id="qti6" value="0" onkeyup="recalculate(this)"></td>
						</tr>
						<tr>
							<td>奖项七:</td>
							<td><select name="prizeList[6].prizeTypeId"  id="prizeType7"><option value="" disabled selected>请选择类型</option><c:forEach items="${prizeTypeList }" var="en"><option value="${en.id }">${en.typeName }</option></c:forEach></select></td>
							<td class="std" >描述:</td>
							<td><input type="text" name="prizeList[6].description" id="prizeList[6].description"></td>
							<td class="std">数值:</td>
							<td><input type="text" class="stext" name="prizeList[6].value" id="prizeList[6].value" value="0"></td>
							<td class="std">数量:</td>
							<td><input type="text" class="stext" name="prizeList[6].qti" id="qti7" value="0" onkeyup="recalculate(this)"></td>
						</tr>
						<tr>
							<td>奖项八:</td>
							<td><select name="prizeList[7].prizeTypeId"  id="prizeType8"><option value="" disabled selected>请选择类型</option><c:forEach items="${prizeTypeList }" var="en"><option value="${en.id }">${en.typeName }</option></c:forEach></select></td>
							<td class="std" >描述:</td>
							<td><input type="text" name="prizeList[7].description" id="prizeList[7].description"></td>
							<td class="std">数值:</td>
							<td><input type="text" class="stext" name="prizeList[7].value" id="prizeList[7].value" value="0"></td>
							<td class="std">数量:</td>
							<td><input type="text" class="stext" name="prizeList[7].qti" id="qti8" value="0" onkeyup="recalculate(this)"></td>
						</tr>	
						<tr> 
							<td colspan="8">
							<input type="submit" value="保存修改" class="sbtn25 sbtn25_1" style="margin-left: 125px;">
							<input type="button" value="返回" class="sbtn25 sbtn25_1" style="margin-left: 325px;" onclick="javascript:history.back(-1);">
							</td>
						</tr>
					</table>
					</form>
				</div>
			</div>
		</div>
	</div>


</body>
</html>